﻿
using FinalTest_Algonrithm;
using HalconDotNet;

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace FinalTest_Algonrithm
{
    public class HuayaLibVision
    {
        /* ******* ******* ******** ******* *******   通用说明   ******** ******* ******* ******* ******* */
        /*
         * 形如 x[m*n]的数组，表示 m 是最外层循环，n 是第二层循环，以此类推
         */

        /* ******* ******* ******** ******* *******   通用设置   ******** ******* ******* ******* ******* */
        /* @hy_load_configs: 加载配置文件，初始化或配置更改时调用
           @varConfigFolder: para, 配置文件路径
           @nStations: para, 站位总数
           @返回值: TRUE(1), ELSE 查看详细代码
         */
        [DllImport("HuayaLibVision.dll")]
        public static extern int hy_load_configs(string varConfigFolder, int nStations);


        /* ******* ******* ******** ******* *******   SFR   ******** ******* ******* ******* ******* */
        /* @hy_AA: AA 清晰度算法
           @iStation: para, 工位编号
           @BmpData[H*W*3]: para, 图像数据缓存
           @OCx[5]: ret, 5个搜索框 x 坐标
           @OCy[5]: ret, 5个搜索框 y 坐标
           @RoiRtX[5*4]: ret, 5个搜索框中4个Roi区域 x 坐标
           @RoiRtY[5*4]: ret, 5个搜索框中4个Roi区域 y 坐标
           @SfrValue[5*4]: ret, 5个搜索框中4个Roi区域清晰度的值
           @SfrAvgValue[5]: ret, 5个搜索框区域平均清晰度的值
           @Freq[]: ret, 绘制 MTF 曲线数据
           @Amp[]: ret, 绘制 MTF 曲线数据
           ******* ******* *******   Freq、Amp 暂不生效   ******** ******* *******
           @返回值: TRUE(1), ELSE 查看详细代码
         */
        [DllImport("HuayaLibVision.dll")]
        public unsafe static extern int hy_AA(int iStation, byte* BmpData, float[] OCx, float[] OCy, float[] RoiRtX, float[] RoiRtY, float[] SfrValue, float[] SfrAvgValue, float[] Freq, float[] Amp);

        /* @hy_get_oc: 获取光学中心 OC
           @iStation: para, 工位编号
           @BmpData[H*W*3]: para, 图像数据缓存
           @OCx: ret, 光学中心 x 坐标
           @OCy: ret, 光学中心 y 坐标
           @返回值: TRUE(1), ELSE 查看详细代码
         */
        [DllImport("HuayaLibVision.dll")]
        public unsafe static extern int hy_get_oc(int iStation, byte* BmpData, ref float OCx, ref float OCy);

        /* ******* ******* ******** ******* *******   FOV   ******** ******* ******* ******* ******* */
        /* @hy_get_fov: 获取视场角
           @iStation: para, 工位编号
           @BmpData[H*W*3]: para, 图像数据缓存
           @HFOV: ret, 横向视场角
           @VFOV: ret, 纵向视场角
           @DFOV: ret, 对角线方向视场角
           @BmpDest: ret, 返回处理后的图像，暂未处理
           @返回值: TRUE(1), ELSE 查看详细代码
         */
        [DllImport("HuayaLibVision.dll")]
        public unsafe static extern int hy_get_fov(int iStation, byte* BmpData, ref float HFOV, ref float VFOV, ref float DFOV, byte* BmpDest);

        /* ******* ******* ******** ******* *******   LIGHT   ******** ******* ******* ******* ******* */
        /* @hy_get_light: 获取亮度值
           @iStation: para, 工位编号
           @BmpData[H*W*3]: para, 图像数据缓存
           @Lightness[5]: ret, 五个搜索框的亮度值
           @返回值: TRUE(1), ELSE 查看详细代码
         */
        [DllImport("HuayaLibVision.dll")]
        public unsafe static extern int hy_get_light(int iStation, byte* BmpData, float[] Lightness);

        /* ******* ******* ******** ******* *******   MIRROR   ******** ******* ******* ******* ******* */
        /* @hy_get_mirror: 获取镜像状态
           ******* ******* *******   方法暂未生效   ******** ******* *******
           @iStation: para, 工位编号
           @BmpData[H*W*3]: para, 图像数据缓存
           @IsMirrored: ret, 是否镜像
           @BmpDest: ret, 返回处理后的图像
           @返回值: TRUE(1), ELSE 查看详细代码
         */
        [DllImport("HuayaLibVision.dll")]
        public unsafe static extern int hy_get_mirror(int iStation, byte* BmpData, ref bool IsMirrored, byte* BmpDest);

        /* ******* ******* ******** ******* *******   AWB   ******** ******* ******* ******* ******* */
        /* @hy_get_awb: 白平衡
          @iStation: para, 工位编号
          @BmpData[H*W*3]: para, 图像数据缓存
          @R[5]: ret, 5 个搜索框的 R 值
          @G[5]: ret, 5 个搜索框的 G 值
          @B[5]: ret, 5 个搜索框的 B 值
          @RvsG[5]: ret, 5 个搜索框的 R:G 值
          @BvsG[5]: ret, 5 个搜索框的 B:G 值
          @BmpDest: ret, 返回处理后的图像
          @返回值: TRUE(1), ELSE 查看详细代码
        */
        [DllImport("HuayaLibVision.dll")]
        public unsafe static extern int hy_get_awb(int iStation, byte* BmpData, float[] R, float[] G, float[] B, float[] RvsG, float[] BvsG, byte* BmpDest);

        /* ******* ******* ******** ******* *******   STAIN   ******** ******* ******* ******* ******* */
        /* @hy_get_stain: 污点测试
          @iStation: para, 工位编号
          @BmpData[H*W*3]: para, 图像数据缓存
          @StainNum: ret, 污点数量
          @StainArea[StainNum]: ret, 污点面积
          ******* ******* *******   StainArea 暂不生效   ******** ******* *******
          @BmpDest: ret, 返回处理后的图像
          @返回值: TRUE(1), ELSE 查看详细代码
        */
        [DllImport("HuayaLibVision.dll")]
        public unsafe static extern int hy_get_stain(int iStation, byte* BmpData, ref int StainNum, int[] StainArea, byte* BmpDest);

        /* ******* ******* ******** ******* *******   BAD POINT   ******** ******* ******* ******* ******* */
        /* @hy_get_bad_point: 坏点测试
          @iStation: para, 工位编号
          @BmpData[H*W*3]: para, 图像数据缓存
          @SmallCount: ret, 1 个坏点数量
          @MediumCount: ret, 2 个坏点数量
          @BigCount: ret, 多个坏点数量
          ******* ******* *******   目前没有区分像素大小，全部加到一起作为返回值   ******** ******* *******
          @BmpDest: ret, 返回处理后的图像
          @返回值: TRUE(1), ELSE 查看详细代码
        */
        [DllImport("HuayaLibVision.dll")]
        public unsafe static extern int hy_get_bad_point(int iStation, byte* BmpData, ref int SmallCount, ref int MediumCount, ref int BigCount, byte* BmpDest);

        /* ******* ******* ******** ******* *******   SHADING   ******** ******* ******* ******* ******* */
        /* @hy_get_shading: 暗角测试
          @iStation: para, 工位编号
          @BmpData[H*W*3]: para, 图像数据缓存
          @RatioUL: ret, 左上角与中心的亮度比值
          @RatioUR: ret, 右上角与中心的亮度比值
          @RatioDL: ret, 左下角与中心的亮度比值
          @RatioDR: ret, 右下角与中心的亮度比值
          @Ratio_Diff: ret, 比值差值
          ******* ******* *******   Ratio_Diff 暂未生效   ******** ******* *******
          @BmpDest: ret, 返回处理后的图像
          @返回值: TRUE(1), ELSE 查看详细代码
        */
        [DllImport("HuayaLibVision.dll")]
        public unsafe static extern int hy_get_shading(int iStation, byte* BmpData, ref float RatioUL, ref float RatioUR, ref float RatioDL, ref float RatioDR, ref float Ratio_Diff, byte* BmpDest);

        /* ******* ******* ******** ******* *******   NOLOCATION   ******** ******* ******* ******* ******* */
        /* @hy_get_nolocation_color6: 色差测试
          @iStation: para, 工位编号
          @BmpData[H*W*3]: para, 图像数据缓存
          @ColorInfo[5]: ret, 5 个搜索框的色差数值
          @BmpDest: ret, 返回处理后的图像
          @返回值: TRUE(1), ELSE 查看详细代码
        */
        [DllImport("HuayaLibVision.dll")]
        public unsafe static extern int hy_get_nolocation_color6(int iStation, byte* BmpData, float[] ColorInfo, byte* BmpDest);
        [DllImport("HuayaLibVision.dll")]
        public unsafe static extern int hy_get_nolocation_color24(int iStation, byte* BmpData, float[] ColorInfo, byte* BmpDest);

        /* ******* ******* ******** ******* *******   GRAY SCALE   ******** ******* ******* ******* ******* */
        /* @hy_get_gray_scale: 灰阶测试
          @iStation: para, 工位编号
          @BmpData[H*W*3]: para, 图像数据缓存
          @GrayInfo[5]: ret, 5 个搜索框的灰度均值
          @GrayDiffInfo[5]: ret, 每个小框和下一个框的灰度均值的差值
          @DiffCount[5]: ret, 灰阶差值个数
          @BmpDest: ret, 返回处理后的图像
          @返回值: TRUE(1), ELSE 查看详细代码
        */
        [DllImport("HuayaLibVision.dll")]
        public unsafe static extern int hy_get_gray_scale(int iStation, byte* BmpData, float[] GrayInfo, float[] GrayDiffInfo, int[] DiffCount, byte* BmpDest);

        /* ******* ******* ******** ******* *******   HOT PIXEL   ******** ******* ******* ******* ******* */
        [DllImport("HuayaLibVision.dll")]
        public unsafe static extern int hy_get_hot_pixel(int iStation, byte* BmpData, ref int SingleCount, ref int DoubleCount, ref int MultipleCount, byte* BmpDest);

        /* ******* ******* ******** ******* *******   IR LED   ******** ******* ******* ******* ******* */
        [DllImport("HuayaLibVision.dll")]
        public unsafe static extern int hy_get_ir_led(int iStation, byte* BmpData, ref float MeanValue, byte* BmpDest);

        /* ******* ******* ******** ******* *******   FLARE   ******** ******* ******* ******* ******* */
        [DllImport("HuayaLibVision.dll")]
        public unsafe static extern int hy_get_flare(int iStation, byte* BmpData, ref float ResultValue, byte* BmpDest);

        /* ******* ******* ******** ******* *******   SNR   ******** ******* ******* ******* ******* */
        /* @hy_get_snr: 信噪比
          @iStation: para, 工位编号
          @BmpData[H*W*3]: para, 图像数据缓存
          @ResultValue: ret, 信噪比数值
          @BmpDest: ret, 返回处理后的图像
          @返回值: TRUE(1), ELSE 查看详细代码
        */
        [DllImport("HuayaLibVision.dll")]
        public unsafe static extern int hy_get_snr(int iStation, byte* BmpData, ref float ResultValue, byte* BmpDest);
    }
    public class Alg_SectorSZ : FinalTest_AlgorithmBase
    {
        public override bool LoadConfig(string path)
        {
            try 
            {
               int res = HuayaLibVision.hy_load_configs(path,4);
                if (res != 1)
                {
                    MessageBox.Show("算法初始化失败");
                    return false;
                }
                return true;
            }
            catch 
            {
                MessageBox.Show("算法初始化异常");
                return false;
            }

          
        }
        public override int GetLight(Bitmap bitmap, float[] Lightness, int iStation = 0)
        {
            try
            {
                int res = -1;
                byte[] Bmpbyte = new byte[bitmap.Width * bitmap.Height *3];
                Bmpbyte = ImageChangeHelper.Instance.ConvertBitmapToByteArray(bitmap);
                unsafe
                {
                    fixed (byte* byteBuffer = Bmpbyte)
                    {
                        res = HuayaLibVision.hy_get_light(iStation, byteBuffer, Lightness);
                    }
                }
                return res;
            }
            catch
            {
               
                return -1;
            }
          
        }
        public override int GetOC(Bitmap bitmap, ref float OCx, ref float OCy, int iStation = 0)
        {
            try
            {
                int res = -1;
                byte[] Bmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                Bmpbyte = ImageChangeHelper.Instance.ConvertBitmapToByteArray(bitmap);
                unsafe
                {
                    fixed (byte* byteBuffer = Bmpbyte)
                    {
                         res = HuayaLibVision.hy_get_oc(iStation, byteBuffer, ref OCx, ref OCy);
                    }
                }
               

                return res;
            }
            catch
            {

                return -1;
            }
            
        }
        public override int GetFOV(Bitmap bitmap, ref float HFOV, ref float VFOV, ref float DFOV, ref Bitmap OutBitmap, int iStation = 0)
         {
            try
            {
                int res;
                byte[] Bmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                byte[] outBmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                Bmpbyte = ImageChangeHelper.Instance.ConvertBitmapToByteArray(bitmap);
                unsafe
                {
                    fixed (byte* byteBuffer = Bmpbyte)
                    {
                        fixed (byte* outbyteBuffer = outBmpbyte)
                        {
                            res = HuayaLibVision.hy_get_fov(iStation, byteBuffer, ref HFOV, ref VFOV, ref DFOV, outbyteBuffer);
                        }

                          
                    }
                }
                OutBitmap = ImageChangeHelper.Instance.ConvertBinaryToBitmap(outBmpbyte, bitmap.Width,bitmap.Height);
                return res;
            }
            catch
            {

                return -1;
            }
        }
        public override int Getmirror(Bitmap bitmap, ref bool IsMirrored, ref Bitmap OutBitmap, int iStation = 0)
        {
            try
            {
                int res;
                byte[] Bmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                byte[] outBmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                Bmpbyte = ImageChangeHelper.Instance.ConvertBitmapToByteArray(bitmap);
                unsafe
                {
                    fixed (byte* byteBuffer = Bmpbyte)
                    {
                        fixed (byte* outbyteBuffer = outBmpbyte)
                        {
                            res = HuayaLibVision.hy_get_mirror(iStation, byteBuffer, ref IsMirrored, outbyteBuffer);
                        }

                    }
                }
                OutBitmap = ImageChangeHelper.Instance.ConvertBinaryToBitmap(outBmpbyte, bitmap.Width, bitmap.Height);
                return res;
            }
            catch
            {

                return -1;
            }
            
        }
        public override bool GetSFRValue(Bitmap bitmap, ref SFRValue SFRValue, ref RectInfo rectInfo, ref LightValue light , int iStation = 0)
        {
            try
            {
                bool res = true;
                byte[] Bmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                byte[] outBmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                Bmpbyte = ImageChangeHelper.Instance.ConvertBitmapToByteArray(bitmap);
                unsafe
                {
                    fixed (byte* byteBuffer = Bmpbyte)
                    {

                        float[] OCx = new float[5];
                        float[] OCy = new float[5];
                        float[] RoiRtX = new float[5];
                        float[] RoiRtY = new float[5];
                        float[] SfrValue = new float[5];
                        float[] SfrAvgValue = new float[5];
                        float[] Freq = new float[5];
                        float[] Amp = new float[5];
                        float[] Lightness = new float[5];
                        int re = HuayaLibVision.hy_AA(iStation, byteBuffer, OCx, OCy, RoiRtX, RoiRtY, SfrValue, SfrAvgValue, Freq, Amp);
                        re = HuayaLibVision.hy_get_light(iStation, byteBuffer, Lightness);
                        if (re != 1)
                        {
                            res = false;
                        }

                        for (int i = 0; i < 5; i++)
                        {
                            SFRValue.block[i].dX = OCx[i];
                            SFRValue.block[i].dY = OCx[i];
                            SFRValue.block[i].dValue = SfrAvgValue[i];
                            SFRValue.block[i].aryValue[0] = SfrAvgValue[i];
                            rectInfo.Points[i][0].X = (int)OCx[i];
                            rectInfo.Points[i][0].Y = (int)OCx[i];
                            light.blockValue[i] = Lightness[i];

                            for (int j = 0; j < 4; j++)
                            {
                                SFRValue.block[i].aryValue[j + 1] = SfrValue[i * 4 + j];
                                rectInfo.Points[i][j + 1].X = (int)RoiRtX[i * 4 + j];
                                rectInfo.Points[i][j + 1].Y = (int)RoiRtY[i * 4 + j];
                            }
                        }


                    }
                }
                return res;
            }
            catch
            {

                return false;
            }
            
        }


        public override int GetAWB(Bitmap bitmap, float[] R, float[] G, float[] B, float[] RvsG, float[] BvsG, ref Bitmap OutBitmap, int iStation = 0)
        {
            try
            {
                int res;
                byte[] Bmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                byte[] outBmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                Bmpbyte = ImageChangeHelper.Instance.ConvertBitmapToByteArray(bitmap);
                unsafe
                {
                    fixed (byte* byteBuffer = Bmpbyte)
                    {
                        fixed (byte* outbyteBuffer = outBmpbyte)
                        {
                            res = HuayaLibVision.hy_get_awb(iStation, byteBuffer, R, G, B, RvsG, BvsG, outbyteBuffer);
                        }

                    }
                }
                OutBitmap = ImageChangeHelper.Instance.ConvertBinaryToBitmap(outBmpbyte, bitmap.Width, bitmap.Height);
                return res;

             
            }
            catch
            {

                return -1;
            }
        }
        public override int GetBlemish(Bitmap bitmap, ref int StainNum, int[] StainArea, ref Bitmap OutBitmap, int iStation = 0)
        {

            try
            {
                int res;
                byte[] Bmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                byte[] outBmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                Bmpbyte = ImageChangeHelper.Instance.ConvertBitmapToByteArray(bitmap);
                unsafe
                {
                    fixed (byte* byteBuffer = Bmpbyte)
                    {
                        fixed (byte* outbyteBuffer = outBmpbyte)
                        {
                            res = HuayaLibVision.hy_get_stain(iStation, byteBuffer, ref StainNum, StainArea, outbyteBuffer);
                        }

                    }
                }
                OutBitmap = ImageChangeHelper.Instance.ConvertBinaryToBitmap(outBmpbyte, bitmap.Width, bitmap.Height);
                return res;

             
            }
            catch
            {

                return -1;
            }
        }
        public override int GetParity(Bitmap bitmap, ref int SmallCount, ref int MediumCount, ref int BigCount, ref Bitmap OutBitmap, int iStation = 0)
        {
            try
            {
                int res;
                byte[] Bmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                byte[] outBmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                Bmpbyte = ImageChangeHelper.Instance.ConvertBitmapToByteArray(bitmap);
                unsafe
                {
                    fixed (byte* byteBuffer = Bmpbyte)
                    {
                        fixed (byte* outbyteBuffer = outBmpbyte)
                        {
                            res = HuayaLibVision.hy_get_bad_point(iStation, byteBuffer, ref SmallCount, ref MediumCount, ref BigCount, outbyteBuffer);
                        }

                    }
                }
                OutBitmap = ImageChangeHelper.Instance.ConvertBinaryToBitmap(outBmpbyte, bitmap.Width, bitmap.Height);
                return res;


            
            }
            catch
            {

                return -1;
            }
        }
        public override int GetShading(Bitmap bitmap, ref float RatioUL, ref float RatioUR, ref float RatioDL, ref float RatioDR, ref float Ratio_Diff, ref Bitmap OutBitmap, int iStation = 0)
        {
            try
            {
                int res;
                byte[] Bmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                byte[] outBmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                Bmpbyte = ImageChangeHelper.Instance.ConvertBitmapToByteArray(bitmap);
                unsafe
                {
                    fixed (byte* byteBuffer = Bmpbyte)
                    {
                        fixed (byte* outbyteBuffer = outBmpbyte)
                        {
                            res = HuayaLibVision.hy_get_shading(iStation, byteBuffer, ref RatioUL, ref RatioUR, ref RatioDL, ref RatioDR, ref Ratio_Diff, outbyteBuffer);
                        }

                    }
                }
                OutBitmap = ImageChangeHelper.Instance.ConvertBinaryToBitmap(outBmpbyte, bitmap.Width, bitmap.Height);
                return res;




            }
            catch
            {

                return -1;
            }
        }
        public override int GetColor6(Bitmap bitmap, float[] ColorInfo, ref Bitmap OutBitmap, int iStation = 0)
        {
            try
            {
                int res;
                byte[] Bmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                byte[] outBmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                Bmpbyte = ImageChangeHelper.Instance.ConvertBitmapToByteArray(bitmap);
                unsafe
                {
                    fixed (byte* byteBuffer = Bmpbyte)
                    {
                        fixed (byte* outbyteBuffer = outBmpbyte)
                        {
                            res = HuayaLibVision.hy_get_nolocation_color6(iStation, byteBuffer, ColorInfo, outbyteBuffer);
                        }

                    }
                }
                OutBitmap = ImageChangeHelper.Instance.ConvertBinaryToBitmap(outBmpbyte, bitmap.Width, bitmap.Height);
                return res;



              
            }
            catch
            {

                return -1;
            }

        }
        public override int GetColor24(Bitmap bitmap, float[] ColorInfo, ref Bitmap OutBitmap, int iStation = 0)
        {
            try
            {
                int res;
                byte[] Bmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                byte[] outBmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                Bmpbyte = ImageChangeHelper.Instance.ConvertBitmapToByteArray(bitmap);
                unsafe
                {
                    fixed (byte* byteBuffer = Bmpbyte)
                    {
                        fixed (byte* outbyteBuffer = outBmpbyte)
                        {
                            res = HuayaLibVision.hy_get_nolocation_color24(iStation, byteBuffer, ColorInfo, outbyteBuffer);
                        }

                    }
                }
                OutBitmap = ImageChangeHelper.Instance.ConvertBinaryToBitmap(outBmpbyte, bitmap.Width, bitmap.Height);
                return res;
            }
            catch
            {

                return -1;
            }

        }
        public override int GeGray(Bitmap bitmap, float[] GrayInfo, float[] GrayDiffInfo, int[] DiffCount, ref Bitmap OutBitmap, int iStation = 0)
        {
            try
            {
                int res;
                byte[] Bmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                byte[] outBmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                Bmpbyte = ImageChangeHelper.Instance.ConvertBitmapToByteArray(bitmap);
                unsafe
                {
                    fixed (byte* byteBuffer = Bmpbyte)
                    {
                        fixed (byte* outbyteBuffer = outBmpbyte)
                        {
                            res = HuayaLibVision.hy_get_gray_scale(iStation, byteBuffer, GrayInfo, GrayDiffInfo, DiffCount, outbyteBuffer);
                        }

                    }
                }
                OutBitmap = ImageChangeHelper.Instance.ConvertBinaryToBitmap(outBmpbyte, bitmap.Width, bitmap.Height);
                return res;

            }
            catch
            {

                return -1;
            }
        }
        public override int GeHotpixel(Bitmap bitmap, ref int SingleCount, ref int DoubleCount, ref int MultipleCount, ref Bitmap OutBitmap, int iStation = 0)
        {
            try
            {
                int res;
                byte[] Bmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                byte[] outBmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                Bmpbyte = ImageChangeHelper.Instance.ConvertBitmapToByteArray(bitmap);
                unsafe
                {
                    fixed (byte* byteBuffer = Bmpbyte)
                    {
                        fixed (byte* outbyteBuffer = outBmpbyte)
                        {
                            res = HuayaLibVision.hy_get_hot_pixel(iStation, byteBuffer, ref  SingleCount, ref  DoubleCount, ref  MultipleCount, outbyteBuffer);
                        }

                    }
                }
                OutBitmap = ImageChangeHelper.Instance.ConvertBinaryToBitmap(outBmpbyte, bitmap.Width, bitmap.Height);
                return res;
            }
            catch
            {

                return -1;
            }
        }
        public override int GeFlare(Bitmap bitmap, ref float ResultValue, ref Bitmap OutBitmap, int iStation = 0)
        {
            try
            {

                int res;
                byte[] Bmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                byte[] outBmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                Bmpbyte = ImageChangeHelper.Instance.ConvertBitmapToByteArray(bitmap);
                unsafe
                {
                    fixed (byte* byteBuffer = Bmpbyte)
                    {
                        fixed (byte* outbyteBuffer = outBmpbyte)
                        {
                            res = HuayaLibVision.hy_get_flare(iStation, byteBuffer, ref ResultValue, outbyteBuffer);
                        }

                    }
                }
                OutBitmap = ImageChangeHelper.Instance.ConvertBinaryToBitmap(outBmpbyte, bitmap.Width, bitmap.Height);
                return res;
             
            }
            catch
            {

                return -1;
            }
        }
        public override int GeSNR(Bitmap bitmap, ref float ResultValue, ref Bitmap OutBitmap, int iStation = 0)
        {
            try
            {
                int res;
                byte[] Bmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                byte[] outBmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                Bmpbyte = ImageChangeHelper.Instance.ConvertBitmapToByteArray(bitmap);
                unsafe
                {
                    fixed (byte* byteBuffer = Bmpbyte)
                    {
                        fixed (byte* outbyteBuffer = outBmpbyte)
                        {
                            res = HuayaLibVision.hy_get_snr(iStation, byteBuffer, ref ResultValue, outbyteBuffer);
                        }

                    }
                }
                OutBitmap = ImageChangeHelper.Instance.ConvertBinaryToBitmap(outBmpbyte, bitmap.Width, bitmap.Height);
                return res;


              
            }
            catch
            {

                return -1;
            }
        }
        public override int GeIRLED(Bitmap bitmap, ref float MeanValue, ref Bitmap OutBitmap, int iStation = 0)
        {
            try
            {
                int res;
                byte[] Bmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                byte[] outBmpbyte = new byte[bitmap.Width * bitmap.Height * 3];
                Bmpbyte = ImageChangeHelper.Instance.ConvertBitmapToByteArray(bitmap);
                unsafe
                {
                    fixed (byte* byteBuffer = Bmpbyte)
                    {
                        fixed (byte* outbyteBuffer = outBmpbyte)
                        {
                            res = HuayaLibVision.hy_get_ir_led(iStation, byteBuffer, ref MeanValue, outbyteBuffer);
                        }

                    }
                }
                OutBitmap = ImageChangeHelper.Instance.ConvertBinaryToBitmap(outBmpbyte, bitmap.Width, bitmap.Height);
                return res;



               
            }
            catch
            {

                return -1;
            }
        }








    }



}
